package com.zm.test;

import com.zm.dao.IAccountDao;
import com.zm.domain.Account;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MyBatisAnnoTestAccount {
    private SqlSessionFactory factory;
    private SqlSession session;
    private IAccountDao accountDao;
    private InputStream in;

    @Before
    public void init() throws IOException {
        //1.获取字节输入流
        in = Resources.getResourceAsStream("SqlMapConfig.xml");

        //2.根据字节输入流构建SqlSessionFactory
        factory=new SqlSessionFactoryBuilder().build(in);

        //3.根据SqlSessionFactory生产一个SqlSession
        session = factory.openSession();

        //4.使用SqlSession获取dao的代理对象
        accountDao = session.getMapper(IAccountDao.class);

    }


    //====================================查询方法==============================================

    /**
     * 测试基于注解的mybatis使用
     * 查询所有人
     *
     */
    @Test
    public void testFindAll() {
        List<Account> accounts=accountDao.findAll();
        for (Account account :accounts){
            System.out.println("======================================每个账户的信息==================================");
            System.out.println(account);
            System.out.println(account.getUser());
        }

    }

/*
    */
/**
     * 根据id查询用户信息
     *//*


    @Test
    public void testFindById(){
     User user= accountDao.findById(41);
        System.out.println(user);
    }

    */
/**
     * 根据用户名进行模糊查询
     *//*


    @Test
    public void testFindByName(){
        List<User> users =accountDao.findUserByName("%"+"王"+"%");
        for (User user : users){
            System.out.println(user);
        }
    }
*/



    @After
    public void destroy() throws IOException {
        //5.事务提交
        session.commit();
        //6.释放资源
        session.close();
        in.close();
    }
}
